package io.itit.ecp.admin.server.service.impl;

import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.server.entity.RunningModeDataEntity;
import io.itit.ecp.admin.server.mapper.RunningModeDataMapper;
import io.itit.ecp.admin.server.service.IRunningModeDataService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Objects;


@Service
public class RunningModeDataServiceImpl extends JoinServiceImpl<RunningModeDataMapper, RunningModeDataEntity>
        implements IRunningModeDataService {

    @Override
    public BigDecimal sumCarbonValue(String sceneEdificeId, LocalDateTime startTime, LocalDateTime endTime) {
        BigDecimal value = Joins.of(RunningModeDataEntity.class)
                .selectAs(builder -> builder.add("SUM(carbon_value)", "carbon_value", false))
                .eq(StringUtils.isNotEmpty(sceneEdificeId), RunningModeDataEntity::getSceneEdificeId, sceneEdificeId)
                .ge(Objects.nonNull(startTime), RunningModeDataEntity::getCreateTm, startTime)
                .le(Objects.nonNull(endTime), RunningModeDataEntity::getSolidifyDate, endTime)
                .joinGetOne(BigDecimal.class);
        return ObjectUtils.defaultIfNull(value, BigDecimal.ZERO).setScale(2, RoundingMode.HALF_DOWN);
    }

    @Override
    public BigDecimal sumIncomeValue(String sceneEdificeId, LocalDateTime startTime, LocalDateTime endTime) {
        BigDecimal value = Joins.of(RunningModeDataEntity.class)
                .selectAs(builder -> builder.add("SUM(income_value)", "income_value", false))
                .eq(StringUtils.isNotEmpty(sceneEdificeId), RunningModeDataEntity::getSceneEdificeId, sceneEdificeId)
                .ge(Objects.nonNull(startTime), RunningModeDataEntity::getSolidifyDate, startTime)
                .le(Objects.nonNull(endTime), RunningModeDataEntity::getSolidifyDate, endTime)
                .joinGetOne(BigDecimal.class);
        return ObjectUtils.defaultIfNull(value, BigDecimal.ZERO).setScale(2, RoundingMode.HALF_DOWN);
    }

    @Override
    public BigDecimal sumSaveElectricityCostsValue(String sceneEdificeId, LocalDateTime startTime, LocalDateTime endTime) {
        BigDecimal value = Joins.of(RunningModeDataEntity.class)
                .selectAs(builder -> builder.add("SUM(save_electricity_costs)", "save_electricity_costs", false))
                .eq(StringUtils.isNotEmpty(sceneEdificeId), RunningModeDataEntity::getSceneEdificeId, sceneEdificeId)
                .ge(Objects.nonNull(startTime), RunningModeDataEntity::getSolidifyDate, startTime)
                .le(Objects.nonNull(endTime), RunningModeDataEntity::getSolidifyDate, endTime)
                .joinGetOne(BigDecimal.class);
        return ObjectUtils.defaultIfNull(value, BigDecimal.ZERO).setScale(2, RoundingMode.HALF_DOWN);
    }
}
